import 'package:family_expense/shared/models/system_setting.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class ThemeDataConfig {
  static ThemeData getTheme({
    required BuildContext context,
    Color? savedColor,
    Color? changedColor,
  }) {
    return ThemeData(
      useMaterial3: true,
      // [colorScheme] is for material3.
      colorScheme: getPrimaryColorScheme(savedColor, changedColor),
      // Defines the default font family.
      fontFamily: 'Microsoft YaHei',
      // This makes the visual density adapt to the platform that you run
      // the app on. For desktop platforms, the controls will be smaller and
      // closer together (more dense) than on mobile platforms.
      visualDensity: VisualDensity.adaptivePlatformDensity,
      appBarTheme: AppBarTheme(
        titleTextStyle: TextStyle(
          fontSize: 16.0,
          color: calculateContrastingColor(
            getPrimaryColor(savedColor, changedColor),
          ),
        ),
        foregroundColor: calculateContrastingColor(
          getPrimaryColor(savedColor, changedColor),
        ),
        backgroundColor: getPrimaryColor(savedColor, changedColor),
        elevation: 4.0,
        shadowColor: Theme.of(context).colorScheme.shadow,
      ),
      // With useMaterial3 = true the default button color is much deeper than expected.
      filledButtonTheme: FilledButtonThemeData(
        style: ButtonStyle(
          backgroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
            if (!WidgetState.values.any((p) => states.contains(p))) {
              return getPrimaryColor(savedColor, changedColor);
            }

            // Use default behavior for other states.
            return null;
          }),
          foregroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
            if (!WidgetState.values.any((p) => states.contains(p))) {
              return calculateContrastingColor(
                getPrimaryColor(savedColor, changedColor),
              );
            }

            // Use default behavior for other states.
            return null;
          }),
        ),
      ),
      progressIndicatorTheme: ProgressIndicatorThemeData(
        color: getPrimaryColor(savedColor, changedColor),
      ),
      switchTheme: SwitchThemeData(
        trackColor: WidgetStateProperty.all<Color>(
          getPrimaryColor(savedColor, changedColor),
        ),
      ),
      listTileTheme: const ListTileThemeData(
        subtitleTextStyle: TextStyle(color: Colors.black54),
        leadingAndTrailingTextStyle: TextStyle(
          fontSize: 14.0,
          color: Color.fromRGBO(97, 97, 97, 1.0),
        ),
      ),
      dividerTheme: const DividerThemeData(
        thickness: 0.3,
        color: Colors.black12,
      ),
    );
  }

  /// Gets current primary color. This is for material2.
  ///
  /// [savedColor] is the color from saved [SystemSetting].
  /// [changedColor] is the color from [PrimaryColorSettingView]
  ///
  /// When app is first loaded the [changedColor] is null, then [savedColor] is returned.
  /// when user changed the color in [PrimaryColorSettingView], then [changedColor] is returned.
  static MaterialColor getPrimaryColor(Color? savedColor, Color? changedColor) {
    Color result;

    if (changedColor != null) {
      result = changedColor;
    } else {
      result = savedColor ?? ThemeSetting.defalutPrimaryColor;
    }

    return createMaterialColor(result);
  }

  /// Gets current primary color scheme. This is for material3.
  ///
  /// [savedColor] is the color from saved [SystemSetting].
  /// [changedColor] is the color from [PrimaryColorSettingView]
  ///
  /// When app is first loaded the [changedColor] is null, then [savedColor] is returned.
  /// when user changed the color in [PrimaryColorSettingView], then [changedColor] is returned.
  static ColorScheme getPrimaryColorScheme(
    Color? savedColor,
    Color? changedColor,
  ) {
    return ColorScheme.fromSeed(
      seedColor: (getPrimaryColor(savedColor, changedColor)),
      surfaceTint: Colors.transparent,
    );
  }
}
